package com.zf.shuashua.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * 广播工具类
 */
public class BroadcastHandler {
    /** 更新我喜欢的作品列表 */
    private final static String ACTION_UPDATE_MINE_VIDEO = "UPDATE_MINE_VIDEO";

    public static String getAction(Context context, String action) {
        return  "com.zf.shuashua" + "." + action;
    }

    public static void sendBroadcast(Context context, String action) {
        context.sendBroadcast(new Intent(getAction(context, action)));
    }

    public  static void registerReceiver(Context context,BroadcastReceiver receiver, String action ) {
        try{
            context.registerReceiver(receiver, new IntentFilter(getAction(context, action)));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 批量注销广播
     * @param context
     * @param receivers
     */
    public static void unregisterReceiver(Context context, BroadcastReceiver ...receivers) {
        try{
            for(BroadcastReceiver receiver: receivers) {
                context.unregisterReceiver(receiver);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void sendUpdateLikeVideoCast(Context context) {
        sendBroadcast(context,ACTION_UPDATE_MINE_VIDEO);
    }

    public static void registerUpdateLikeVideoReceiver(Context context, BroadcastReceiver receiver) {
        registerReceiver(context, receiver, ACTION_UPDATE_MINE_VIDEO);
    }

}
